package cn.fengshenghospital.shiro;

import cn.fengshenghospital.entity.User;
import cn.fengshenghospital.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

public class Realm extends AuthorizingRealm {
    @Autowired
    private UserService userService;
    //登陆
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token){
        try {
            User user = userService.login(token.getPrincipal().toString());
            if (user!=null){
                Session session = SecurityUtils.getSubject().getSession();
                session.setAttribute("user",user);
                AuthenticationInfo info = new SimpleAuthenticationInfo(user,user.getUserPassword(),"login");
                return info;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        info.addStringPermission("menu:queryMenu");
        Subject subject = SecurityUtils.getSubject();
//        User user = subject.getPrincipal();
        return info;
    }


}
